#include"grafsubs.h"





SimpleDrawer::SimpleDrawer(QGraphicsScene *_img):
    img(_img),openeds(NULL)
{
    height=img->sceneRect().height();
    width=img->sceneRect().width();
    MoveTo(0,0);
}

SimpleDrawer::~SimpleDrawer()
{
    delete openeds;
}

QColor ColorTranslation(long internalcol)
{
    QColor res;
    switch(internalcol)
    {
        case mCBLACK: res=Qt::black; break;
        case mCBLUE: res=Qt::blue; break;
        case mCGREEN: res=Qt::green; break;
        case mCGRAY: res=Qt::lightGray;break;
        default: res=Qt::white; break;
    }
    return res;
}



void SimpleDrawer::SetPColor(long c)
{
    pen.setColor(ColorTranslation(c));
}
void SimpleDrawer::SetPStyle(int s)
{
    if(s==mSSOLID)
        pen.setStyle(Qt::SolidLine);
    else
        pen.setStyle(Qt::DashLine);

}
void SimpleDrawer::SetPWidth(int w)
{
    pen.setWidth(w);
}
void SimpleDrawer::SetFColor(long c)
{
    fcol=c;
}
void SimpleDrawer::SetFSize(int h)
{
    fsiz=h;
}
void SimpleDrawer::MoveTo(int x,int y)
{
    delete openeds;
    openeds=new QPainterPath;
    openeds->moveTo(x,y);
}
void SimpleDrawer::LineTo(int x,int y)
{
    openeds->lineTo(x,y);
    img->addPath(*openeds,pen);
    MoveTo(x,y);
}




void SimpleDrawer::Draw()
{
    SetPColor(mCBLACK);
    SetPStyle(mSSOLID);
    vector<Triangle> triags=Fractal(width,height);
    for(unsigned int i=0;i<triags.size();i++)
        DrawTriangle(triags[i]);

/*    Triangle tri(M_PI/3,Coord(0,0),Segment(Coord(width/2-100,height/2-50),Coord(width/2+100,height/2-50)));
    DrawTriangle(tri);
    Coord centre=tri.Center();
    vector<Segment> segs=tri.Borders();
    DrawCoord(centre);
    for(unsigned int i=0;i<3;i++)
        DrawTriangle(Triangle(M_PI/4,centre,segs[i]));

/*    Triangle tri1(M_PI/3,Segment(Coord(width/2+100,height/2-50),Coord(width/2-100,height/2-50)));
    DrawTriangle(tri1);*/

}

void SimpleDrawer::DrawTriangle(const Triangle& tri)
{
    SetPWidth(tri.width);
    MoveTo((int)tri.c1.x,(int)tri.c1.y);
    LineTo((int)tri.c2.x,(int)tri.c2.y);
    LineTo((int)tri.c3.x,(int)tri.c3.y);
    LineTo((int)tri.c1.x,(int)tri.c1.y);
}

void SimpleDrawer::DrawLine(const Line& lin)
{
    Coord c[2];
    int count=0;
    Coord top=Intersection(lin,Line(Segment(Coord(0,0),Coord(100,0))));
    Coord left=Intersection(lin,Line(Segment(Coord(0,0),Coord(0,100))));
    Coord bottom=Intersection(lin,Line(Segment(Coord(0,height),Coord(100,height))));
    Coord right=Intersection(lin,Line(Segment(Coord(width,0),Coord(width,100))));
    if(top.x>0&&top.x<=width&&count<2)
        c[count++]=top;
    if(left.y>0&&left.y<=height&&count<2)
        c[count++]=left;
    if(bottom.x>0&&bottom.y<=width&&count<2)
        c[count++]=bottom;
    if(right.y>0&&right.y<=height&&count<2)
        c[count++]=right;
    MoveTo((int)c[0].x,(int)c[0].y);
    LineTo((int)c[1].x,(int)c[1].y);
};

void SimpleDrawer::DrawCoord(const Coord& coo)
{
    MoveTo((int)coo.x,(int)coo.y-5);
    LineTo((int)coo.x,(int)coo.y+5);
    MoveTo((int)coo.x-5,(int)coo.y);
    LineTo((int)coo.x+5,(int)coo.y);
};



